package com.zyx.crawlerdemo.httpclient.crawler;

import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * @author Yaxi.Zhang
 * @since 2021/8/11 17:10
 * desc: httpclient设置头信息案例1
 */
public class HttpclientSetHeader1 {

	public static void main(String[] args) throws IOException {
		// 初始化httpclient
		HttpClient httpClient = HttpClients.custom().build();
		// 使用的请求方法
		HttpGet httpget = new HttpGet("https://searchcustomerexperience.techtarget.com/info/news");
		// 请求头配置
		httpget.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
		httpget.setHeader("Accept-Encoding", "gzip, deflate");
		httpget.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
		httpget.setHeader("Cache-Control", "max-age=0");
		httpget.setHeader("Host", "searchcustomerexperience.techtarget.com");
		// 这项内容很重要
		String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36";
		httpget.setHeader("User-Agent", userAgent);
		// 发出get请求
		HttpResponse response = httpClient.execute(httpget);
		// 获取响应状态码
		int code = response.getStatusLine().getStatusCode();
		// 获取网页内容流
		HttpEntity httpEntity = response.getEntity();
		// 以字符串的形式(需设置编码)
		String entity = EntityUtils.toString(httpEntity, "gbk");
		// 输出所获得的的内容
		System.out.println(code + "\n" + entity);
		// 关闭内容流
		EntityUtils.consume(httpEntity);
	}

}
